<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="权限管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="800px"
    >
        <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="权限名称" prop="title">
                <el-input v-model="form.title"/>
            </el-form-item>
            <el-form-item label="权限ID" prop="permissionId">
                <el-input v-model="form.permissionId"/>
            </el-form-item>
            <el-form-item label="接口" prop="router">
                <el-input v-model="form.router" type="textarea" autosize/>
            </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false" style="margin-right: 20px;">取 消</el-button>
            <el-popconfirm title="确定保存吗？" @onConfirm="submitForm()">
                <el-button slot="reference" type="primary">保存</el-button>
            </el-popconfirm>
        </div>
    </el-dialog>
</template>

<script>
import {PermissionApi} from '@/api/system/PermissionApi'
import {Common} from '@/utils/common'

export default {
    name: 'SystemMenuUpdate',
    components: {},
    data() {
        return {
            form: {
                id: '',
                parentId: 0,
                permissionId:'',
                title: '',
                router:'[]'
            },
            rules: {
                title: [
                    {required: true, message: '请输入权限名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ],
                permissionId: [
                    {required: true, message: '请输入权限ID标识', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            componentPathReadonly:false,
            apiUrl:''
        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            this.$nextTick(() => {
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }

                if (data.id) {
                    this.form = Common.assign(this.form, data)
                    this.form.router = JSON.stringify(this.form.router, null, 4)
                } else {
                    this.form.id = 0
                    this.form.parentId = data.parentId
                    this.form.router = '[]'
                }
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    let data = Common.deepClone(that.form)
                    data.router = JSON.parse(data.router)

                    if (this.form.id) {
                        PermissionApi.edit(data)
                            .then((response) => {
                                that.dialogVisible = false
                                that.$emit('refresh',data.parentId)
                            })
                    } else {
                        PermissionApi.add(data)
                            .then((response) => {
                                that.dialogVisible = false
                                that.$emit('refresh',data.parentId)
                            })
                    }
                }
            })
        }
    }
}
</script>

<style>
    .api-list{ line-height:24px;}
    .api-list .item{ display:flex;}
    .api-list .item:hover{ background:#EEE;}
    .api-list .item .left{ flex:1;}
    .api-list .item .right{ cursor: pointer; }
    .api-list .item .right .el-icon-close{ font-size:16px; }
</style>
